﻿using System;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Ribbon;
using TShooter.TfsWord.AddIn.UIManager.Word;
using CustomTaskPane=Microsoft.Office.Tools.CustomTaskPane;
using System.Threading;
using TShooter.TfsWord.Resources;

namespace TShooter.TfsWord.AddIn
{
    public partial class ThisAddIn
    {
        private static CultureInfo UICulture { get; set; }

        private void ThisAddIn_Startup(object sender, EventArgs e)
        {
            // combine with existing windows
            foreach (Window wnd in Application.Windows)
            {
                Application_WindowActivate(wnd.Document, wnd);
            }

            // hook the WindowActivate & WindowDeactivate event.
            Application.WindowActivate += Application_WindowActivate;
            Application.DocumentChange += Application_DocumentChange;
        }

        protected override OfficeRibbon[] CreateRibbonObjects()
        {
            SetUICulture();
            return new OfficeRibbon[] { new TeamRibbon() };
        }

        private void SetUICulture()
        {
            var app = GetHostItem<Microsoft.Office.Interop.Word.Application>(typeof(Microsoft.Office.Interop.Word.Application), "Application");
            var wordLangSettings = app.LanguageSettings;
            var langId = wordLangSettings.get_LanguageID(MsoAppLanguageID.msoLanguageIDUI);
            UICulture = CultureInfo.GetCultureInfo(langId);

            Thread.CurrentThread.CurrentUICulture = UICulture;
        }

        private void Application_WindowActivate(Document Doc, Window Wn)
        {
            if (Globals.UIManager[Wn].CustomTaskPanes.IsEmpty)
            {
                // This is a new window, associate task pane with it.
                CombineUIElements(Wn, new WorkItemFieldList(), Strings.WorkItemsTaskPaneTitle);
            }

            if (Application.ShowWindowsInTaskbar) // SDI
            {
                foreach (var pane in Globals.UIManager[Wn].CustomTaskPanes)
                {
                    // Update toggle button control status for all CTPs on this window.
                }
            }
            else // MDI
            {
                MdiSwitch(Wn);
            }

            SyncRibbonToDocument(Application.ActiveDocument);
        }

        private void Application_DocumentChange()
        {
            // disable ribbon buttons if no doc open
            var count = Application.Documents.Count;

            //Globals.Ribbons.TeamRibbon.tgbPicturePane.Enabled = count > 0;
            //Globals.Ribbons.TeamRibbon.tgbInfoPane.Enabled = count > 0;

            //if (0 == count)
            //{
            //    Globals.Ribbons.TeamRibbon.tgbPicturePane.Checked = false;
            //    Globals.Ribbons.TeamRibbon.tgbInfoPane.Checked = false;
            //}

            if (count > 0)
            {
                SyncRibbonToDocument(Application.ActiveDocument);
            }
        }

        private void CTP_VisibleChanged(object sender, EventArgs e)
        {
            var ctp = (CustomTaskPane) sender;

            if (Application.ShowWindowsInTaskbar ||
                Globals.UIManager[Application.ActiveWindow].CustomTaskPanes.WithControl(ctp.Control).Contains(ctp))
            {
                // A CustomTaskPane has been shown or hidden, update the toggle button status...
            }
        }

        private void ThisAddIn_Shutdown(object sender, EventArgs e)
        {
            // unhook the WindowActivate & WindowDeactivate event.
            Application.WindowActivate -= Application_WindowActivate;
            Application.DocumentChange -= Application_DocumentChange;
        }


        /// <summary>
        /// Associate a user control as CTP with a window.
        /// </summary>
        /// <param name="window"></param>
        /// <param name="control"></param>
        /// <param name="title"></param>
        private void CombineUIElements(Window window, UserControl control, string title)
        {
            UIContainer container = Globals.UIManager[window];
            CustomTaskPane pane = CustomTaskPanes.Add(control, title, window); // add user control to CTP
            pane.VisibleChanged += CTP_VisibleChanged; // hook the CTP VisibleChanged event.
        }

        private static void SyncRibbonToDocument(Document doc)
        {
            if (Globals.Ribbons.TeamRibbon != null)
            {
                Globals.Ribbons.TeamRibbon.SyncToDocument(doc);
            }

            //if (pane.Control is PicturePicker)
            //{
            //    Globals.Ribbons.TeamRibbon.tgbPicturePane.Checked = pane.Visible;
            //}
            //else if (pane.Control is ComputerInfo)
            //{
            //    Globals.Ribbons.TeamRibbon.tgbInfoPane.Checked = pane.Visible;
            //}
        }

        /// <summary>
        /// In MDI mode, the CustomTaskPane's visibility are decided by the active window & ribbon button.
        /// </summary>
        /// <param name="Wn"></param>
        private void MdiSwitch(Window Wn)
        {
            //bool tgbPicChecked = Globals.Ribbons.TeamRibbon.tgbPicturePane.Checked;
            //bool tgbInfoChecked = Globals.Ribbons.TeamRibbon.tgbInfoPane.Checked;

            //foreach (UIContainer container in Globals.UIManager.UIContainers)
            //{
            //    if (Wn != container.Window)
            //    {
            //        foreach (CustomTaskPane ctp in container.CustomTaskPanes)
            //        {
            //            ctp.Visible = false;
            //        }
            //    }
            //    else
            //    {
            //        WindowCustomTaskPaneCollection<Window> ctps = container.CustomTaskPanes;

            //        ctps.WithControl<PicturePicker>().First().Visible = tgbPicChecked;
            //        ctps.WithControl<ComputerInfo>().First().Visible = tgbInfoChecked;
            //    }
            //}
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            Startup += ThisAddIn_Startup;
            Shutdown += ThisAddIn_Shutdown;
        }

        #endregion
    }
}